const { exec,escape } = require('../../mysql')


// 获取所有订单信息 get 无参
const getOrderLists = () => {
    let sql = `select * from orders;`
    return exec(sql)
}

// 分页获取所有订单 post 参数：page，pageSize
const pageGetOrderLists = (page,pageSize) => {
    let sql = `select * from orders limit ${(page-1)*pageSize},${pageSize};`
    return exec(sql)
}

// 获取所有不同类型的订单  post 参数：状态 page pageSize
const getPaidOrderLists = (status) => {
    let sql = `select * from orders where status = '${status}';`
    return exec(sql)
}


// 分页获取不同类型的订单  post 参数：状态 page pageSize
const pageGetPaidOrderLists = (status,page,pageSize) => {
    let sql = `select * from orders where status = '${status}' limit ${(page-1)*pageSize},${pageSize};`
    return exec(sql)
}

// 新增订单  post  参数：orderTime,status,serverTime,customerId,staffId,payment,address,phone,remark
const addOrder = (orderTime,serverTime,status,customerId,staffId,payment,address,phone,remark) => {
    let sql = `insert into orders (orderTime,serverTime,status,customerId,staffId,payment,address,phone,remark) values(
        '${orderTime}','${serverTime}','${status}',${customerId},${staffId},${payment},'${address}',${phone},'${remark}'
        );
    `
    return exec(sql).then(insertData => {
        if (insertData.affectedRows > 0) {
            return true
        }
        return false
    })
}


// 待支付订单转换成代派单订单
const ToBePaid = (ToBePaidOrderNum,orderTime) => {
    let sql = `update orders set orderTime = '${orderTime}',status = '待派单' where orderNum = ${ToBePaidOrderNum};
    `
    return exec(sql).then(Data => {
        if (Data.affectedRows > 0) {
            return true
        }
        return false
    })
}


// 待派单订单转换成派送中订单
const WaitingList = (ToBePaidOrderNum,serverTime) => {
    let sql = `update orders set serverTime = '${serverTime}',status = '派送中' where orderNum = ${ToBePaidOrderNum};
    `
    return exec(sql).then(Data => {
        if (Data.affectedRows > 0) {
            return true
        }
        return false
    })
}


// 派送中订单转换成已完成订单

const delivery = (ToBePaidOrderNum,serverTime) => {
    let sql = `update orders set serverTime = '${serverTime}',status = '已完成' where orderNum = ${ToBePaidOrderNum};
    `
    return exec(sql).then(Data => {
        if (Data.affectedRows > 0) {
            return true
        }
        return false
    })
}


module.exports = {
    getOrderLists,
    pageGetOrderLists,
    addOrder,
    ToBePaid,
    WaitingList,
    delivery,
    getPaidOrderLists,
    pageGetPaidOrderLists
}